package com.bracketbird.server.repository;

import com.bracketbird.client.service.rtc.*;
import com.bracketbird.server.jdo.club.*;
import com.google.appengine.api.datastore.*;
import com.bracketbird.clientcore.appcontrol.*;
import com.bracketbird.clientcore.model.*;
import com.bracketbird.clientcore.model.keys.*;

import java.util.*;


/**
 *
 */
public class RTCEventConverter {


    private Key createModelKey(RTCEvent e) {
        int type = e.getType();

        //TEAM
        if (type == RTCEventType.addTeam.value()) {
            return KeyFac.createPlayingTeamKey(e.getTournamentId());
        }
        else if (type == RTCEventType.deleteTeam.value()) {
            return KeyFac.convert(e.getModelId());
        }
        else if (type == RTCEventType.updateTeam.value()) {
            return KeyFac.convert(e.getModelId());
        }

        //LEVEL
        else if (type == RTCEventType.addLevel.value()) {
            return KeyFac.createSubtournamentKey(e.getTournamentId());
        }
        else if (type == RTCEventType.updateLevel.value()) {
            return KeyFac.convert(e.getModelId());
        }
        else if (type == RTCEventType.deleteLevel.value()) {
            return KeyFac.convert(e.getModelId());
        }
        else if (type == RTCEventType.finishedLevel.value()) {
            return KeyFac.convert(e.getModelId());
        }
        else if (type == RTCEventType.layoutMatches.value()) {
            return KeyFac.convert(e.getModelId());
        }
        else if (type == RTCEventType.updateMatch.value()) {
            return KeyFac.convert(e.getModelId());
        }

        else if (type == RTCEventType.createTournament.value()) {
            return null;
        }

        else {
            throw new SystemException("Creation of modelKey not implemented for event:" + e.getName());
        }

    }

    private EntityId createModelId(RTCEventJDO jdo) {
        int type = jdo.getEventType();


        //TEAM
        if (type == RTCEventType.addTeam.value()) {
            return KeyFac.getPlayingTeamId(jdo.getModelKey());
        }
        else if (type == RTCEventType.deleteTeam.value()) {
            return KeyFac.getPlayingTeamId(jdo.getModelKey());
        }
        else if (type == RTCEventType.updateTeam.value()) {
            return KeyFac.getPlayingTeamId(jdo.getModelKey());
        }

        //LEVEL
        else if (type == RTCEventType.addLevel.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.deleteLevel.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.updateLevel.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.updateLevel.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.finishedLevel.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.layoutMatches.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }
        else if (type == RTCEventType.updateMatch.value()) {
            return KeyFac.getSubtournamentId(jdo.getModelKey());
        }


        else if (type == RTCEventType.createTournament.value()) {
            return null;
        }

        else {
            throw new SystemException("Creation of modelId not implemented for event:" + jdo.getEventName());
        }

    }


    public RTCEventJDO convert(RTCEvent e, long newEventLogId) {
        RTCEventJDO jdo = new RTCEventJDO();
        jdo.setKey(KeyFac.createRTCEventKey(e.getTournamentId()));
        jdo.setTournamentKey(KeyFac.convert(e.getTournamentId()));

        jdo.setLocalId(e.getLocalId());
        jdo.setModelKey(createModelKey(e));
        jdo.setCreatedDate(new Date());
        jdo.setEventName(e.getName());
        jdo.setEventType(e.getType());
        jdo.setId(newEventLogId);
        jdo.setState(e.getState());

        jdo.setMatchVO(e.getMatchVO());
        jdo.setPlayingTeamVO(e.getPlayingTeamVO());
        jdo.setTournamentLevelVO(e.getTournamentLevelVO());
        jdo.setTournamentVO(e.getTournamentVO());
        jdo.setLevelFinishedVO(e.getLevelFinishedVO());

        jdo.setDeleted(false);
        return jdo;
    }


    public List<RTCEvent> convert(Collection<RTCEventJDO> jdos) {
        List<RTCEvent> eList = new ArrayList<RTCEvent>();
        for (RTCEventJDO jdo : jdos) {
            eList.add(convert(jdo));
        }
        return eList;
    }

    public RTCEvent convert(RTCEventJDO jdo) {
        RTCEvent e = new RTCEvent();
        e.setId(KeyFac.getRTCEventId(jdo.getKey()));

        e.setModelId(createModelId(jdo));
        e.setLocalId(jdo.getLocalId());
        e.setCreatedDate(jdo.getCreatedDate());
        e.setName(jdo.getEventName());
        e.setType(jdo.getEventType());
        e.setEventLogId(jdo.getId());
        e.setState(jdo.getState());

        e.setMatchVO(jdo.getMatchVO());
        e.setPlayingTeamVO(jdo.getPlayingTeamVO());
        e.setTournamentLevelVO(jdo.getTournamentLevelVO());
        e.setTournamentVO(jdo.getTournamentVO());
        e.setLevelFinishedVO(jdo.getLevelFinishedVO());
        e.setDeleted(jdo.getDeleted());
        return e;
    }


}